package com.ruicar.afs.cloud.afscase.infomanagement.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approveprev.entity.CaseApprovePrevInfo;
import com.ruicar.afs.cloud.afscase.approveprev.service.CaseApprovePrevInfoService;
import com.ruicar.afs.cloud.afscase.approvetask.condition.WorkTaskPoolCondition;
import com.ruicar.afs.cloud.afscase.approvetask.vo.CustInfoVo;
import com.ruicar.afs.cloud.afscase.approvetask.vo.CustSummaryInfoVo;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelWitnessInfo;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelWitnessInfoService;
import com.ruicar.afs.cloud.afscase.infomanagement.condition.CaseInfoQueryCondition;
import com.ruicar.afs.cloud.afscase.infomanagement.condition.CreditCustomerCondition;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.*;
import com.ruicar.afs.cloud.afscase.infomanagement.service.*;
import com.ruicar.afs.cloud.afscase.infomanagement.vo.*;
import com.ruicar.afs.cloud.afscase.loanactivatepool.entity.LoanActivatePool;
import com.ruicar.afs.cloud.afscase.loanactivatepool.service.LoanActivateService;
import com.ruicar.afs.cloud.aggregate.dto.CertNoDto;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinCostDetails;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.*;
import com.ruicar.afs.cloud.common.modules.contract.cms.vo.BasicCustBaseInfoVo;
import com.ruicar.afs.cloud.common.modules.contract.cms.vo.CustBaseInfoVo;
import com.ruicar.afs.cloud.parameter.commom.service.TsysAddressParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Description:
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-05-21 20:52
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("客户信息")
@RequestMapping("/caseCustInfo")
public class CaseCustInfoController {

    private final CaseCustInfoService caseCustInfoService;
    private final CaseCustIndividualService caseCustIndividualService;
    private final CaseCustContactService caseCustContactService;
    private final CaseCustAddressService caseCustAddressService;
    private final CaseCarInfoService caseCarInfoService;
    private final CaseCostInfoService caseCostInfoService;
    private final TsysAddressParamService tsysAddressParamService;
    private final CaseBaseInfoService caseBaseInfoService;
    private final CaseRedundantInfoService redundantInfoService;
    private final CaseApprovePrevInfoService caseApprovePrevInfoService;
    private final CaseContractInfoService caseContractInfoService;
    private final LoanActivateService loanActivateService;
    private final ChannelWitnessInfoService channelWitnessInfoService;

    /**
     * queryPageArea
     * <p>Description: 根据申请编号查询是否存在共借人或担保人</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryPageArea")
    @ApiOperation(value = "根据申请编号查询页面显示要素")
    public IResponse<CustPageAreaVo> queryPageArea(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {

        CustPageAreaVo custPageAreaVo = new CustPageAreaVo();
        /**  共借人  **/
        CaseCustInfo commonCustInfo = caseCustInfoService.getOne(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getApplyNo, caseInfoQueryCondition.getApplyNo())
                .eq(CaseCustInfo::getCustRole, CustRoleEnum.COMMONCSUT.getCode()));
        custPageAreaVo.setCommon(ObjectUtils.isNotEmpty(commonCustInfo));
        /**  担保人  **/
        CaseCustInfo guranCustInfo = caseCustInfoService.getOne(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getApplyNo, caseInfoQueryCondition.getApplyNo())
                .eq(CaseCustInfo::getCustRole, CustRoleEnum.GUARANTOR.getCode()));
        custPageAreaVo.setGuaran(ObjectUtils.isNotEmpty(guranCustInfo));
        return IResponse.success(custPageAreaVo);
    }

    /**
     * queryApproveTaskList
     * <p>Description:主借人基本信息查询 </p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryMainBaseInfo")
    @ApiOperation(value = "按照申请编号查询主借人基本信息")
    public IResponse<MainBaseInfoVo> queryApproveTaskList(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {
        MainBaseInfoVo mainBaseInfoVo = new MainBaseInfoVo();
        /**  客户信息  **/
        CaseCustInfo caseCustInfo = caseCustInfoService.getOne(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getApplyNo, caseInfoQueryCondition.getApplyNo())
                .eq(CaseCustInfo::getCustRole, CustRoleEnum.MIANCUST.getCode()));
        if (ObjectUtils.isNotEmpty(caseCustInfo)) {
            BeanUtils.copyProperties(caseCustInfo, mainBaseInfoVo);
            mainBaseInfoVo.setId(caseCustInfo.getId().toString());
            /**  个人客户信息  **/
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(Wrappers.<CaseCustIndividual>query().lambda()
                    .eq(CaseCustIndividual::getCustId, caseCustInfo.getId()));
            if (ObjectUtils.isNotEmpty(caseCustIndividual)) {
                BeanUtils.copyProperties(caseCustIndividual, mainBaseInfoVo);
            }
            /**  户籍地址  **/
            List<CaseCustAddress> caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                    .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                    .eq(CaseCustAddress::getAddressType, AddressTypeEnum.CENSUS.getCode()));
            if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                String province = tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getProvince()) != null ? tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getProvince()).getLabel() : "";
                String city = tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getCity()) != null ? tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getCity()).getLabel() : "";
                mainBaseInfoVo.setCensus(province + city);
                mainBaseInfoVo.setProvince(caseCustAddressList.get(0).getProvince() != null ? caseCustAddressList.get(0).getProvince() : "");
                mainBaseInfoVo.setCity(caseCustAddressList.get(0).getCity() != null ? caseCustAddressList.get(0).getCity() : "");
            }
            List list=this.getEvidenceIndividual(caseInfoQueryCondition.getApplyNo(),caseCustInfo);
            if(CollectionUtils.isNotEmpty(list)){
                RedundantSignVo signVo= JSONObject.parseObject(JSONObject.toJSONString(list.get(0)),RedundantSignVo.class);
                CaseCustInfo custInfo= JSONObject.parseObject(JSONObject.toJSONString(list.get(1)),CaseCustInfo.class);
                CaseCustIndividual individual= JSONObject.parseObject(JSONObject.toJSONString(list.get(2)),CaseCustIndividual.class);
                if(ObjectUtils.isNotEmpty(custInfo)){
                    mainBaseInfoVo.setTelPhoneOld(custInfo.getTelPhone());
                }
                if(ObjectUtils.isNotEmpty(individual)){
                    mainBaseInfoVo.setMaritalStatusOld(individual.getMaritalStatus());
                }
                mainBaseInfoVo.setBackSign(signVo.getBackSign());
                mainBaseInfoVo.setReconsiderSign(signVo.getReconsiderSign());


            }
        }
        return IResponse.success(mainBaseInfoVo);
    }

    /**
     * queryMainWorkInfo主借人工作信息查询
     * <p>Description:queryMainWorkInfo </p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryMainWorkInfo")
    @ApiOperation(value = "按照申请编号查询主借人工作信息")
    public IResponse<MainWorkInfoVo> queryMainWorkInfo(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {
        MainWorkInfoVo mainWorkInfoVo = new MainWorkInfoVo();
        /**  客户信息  **/
        CaseCustInfo caseCustInfo = caseCustInfoService.getOne(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getApplyNo, caseInfoQueryCondition.getApplyNo())
                .eq(CaseCustInfo::getCustRole, CustRoleEnum.MIANCUST.getCode()));
        if (ObjectUtils.isNotEmpty(caseCustInfo)) {
            BeanUtils.copyProperties(caseCustInfo, mainWorkInfoVo);
            mainWorkInfoVo.setId(caseCustInfo.getId().toString());
            /**  个人客户信息  **/
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(Wrappers.<CaseCustIndividual>query().lambda()
                    .eq(CaseCustIndividual::getCustId, caseCustInfo.getId()));
            if (ObjectUtils.isNotEmpty(caseCustIndividual)) {
                BeanUtils.copyProperties(caseCustIndividual, mainWorkInfoVo);
                mainWorkInfoVo.setFamilyIncome(caseCustIndividual.getOtherIncome());
            }
        }
        List list=this.getEvidenceIndividual(caseInfoQueryCondition.getApplyNo(),caseCustInfo);
        if(CollectionUtils.isNotEmpty(list)){
            RedundantSignVo signVo= JSONObject.parseObject(JSONObject.toJSONString(list.get(0)),RedundantSignVo.class);
            CaseCustIndividual individual= JSONObject.parseObject(JSONObject.toJSONString(list.get(2)),CaseCustIndividual.class);
            if(ObjectUtils.isNotEmpty(individual)){
                mainWorkInfoVo.setUnitNameOld(individual.getUnitName());
            }
            mainWorkInfoVo.setBackSign(signVo.getBackSign());
            mainWorkInfoVo.setReconsiderSign(signVo.getReconsiderSign());
        }
        return IResponse.success(mainWorkInfoVo);
    }
    /**
     * queryContactList
     * <p>Description: 查询联系人列表</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryContactList")
    @ApiOperation(value = "按照申请编号查询联系人列表")
    public IResponse<List<ContactVo>> queryContactList(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {
        List<ContactVo> contactVoList = new ArrayList<>();

        /**  联系人信息列表  **/
        List<CaseCustContact> caseCustContactList = caseCustContactService.list(Wrappers.<CaseCustContact>query().lambda()
                .eq(CaseCustContact::getApplyNo, caseInfoQueryCondition.getApplyNo()));
        CaseRedundantInfo redundantInfo = redundantInfoService.getOne(Wrappers.<CaseRedundantInfo>query().lambda()
                .eq(StringUtils.isNotBlank(caseInfoQueryCondition.getApplyNo()), CaseRedundantInfo::getApplyNo, caseInfoQueryCondition.getApplyNo()));
        if (CollectionUtils.isNotEmpty(caseCustContactList)) {
            for (CaseCustContact caseCustContact : caseCustContactList) {
                ContactVo contactVo = new ContactVo();
                contactVo.setBackSign(WhetherEnum.NO.getCode());
                contactVo.setReconsiderSign(WhetherEnum.NO.getCode());
                BeanUtils.copyProperties(caseCustContact, contactVo);
                contactVoList.add(contactVo);

                if (ObjectUtils.isNotEmpty(redundantInfo)) {
                    String backSign = redundantInfo.getBackSign();
                    if (WhetherEnum.YES.getCode().equals(backSign)) {
                        String backEvidence = redundantInfo.getBackEvidence();
                        if (StringUtils.isNotBlank(backEvidence)) {
                            JSONObject jsonObject = JSONObject.parseObject(backEvidence);
                            JSONArray contactList = jsonObject.getJSONArray("caseContactList");
                            if (ObjectUtils.isNotEmpty(contactList) && contactList.size() > 0){
                                List<CaseCustInfo> list=contactList.toJavaList(CaseCustInfo.class);
                                contactVoList.forEach(contactVo1 -> {
                                    for (CaseCustInfo caseCustInfo : list) {
                                        if (contactVo1.getId().equals(caseCustInfo.getId())){
                                            contactVo.setCustNameOld(caseCustInfo.getCustName());
                                            contactVo.setTelPhoneOld(caseCustInfo.getTelPhone());
                                            contactVo.setBackSign(WhetherEnum.YES.getCode());
                                            contactVo.setReconsiderSign(WhetherEnum.NO.getCode());
                                        }
                                    }
                                });
                            }
                        }
                    }
                    String reconsiderSign = redundantInfo.getReconsiderSign();
                    if (WhetherEnum.YES.getCode().equals(reconsiderSign)) {
                        String reconsiderEvidence = redundantInfo.getReconsiderEvidence();
                        if (StringUtils.isNotBlank(reconsiderEvidence)) {
                            JSONObject jsonObject = JSONObject.parseObject(reconsiderEvidence);
                            JSONArray contactList = jsonObject.getJSONArray("caseContactList");
                            if (ObjectUtils.isNotEmpty(contactList) && contactList.size() > 0){
                                List<CaseCustInfo> list=contactList.toJavaList(CaseCustInfo.class);
                                contactVoList.forEach(contactVo1 -> {
                                    for (CaseCustInfo caseCustInfo : list) {
                                        if (contactVo1.getId().equals(caseCustInfo.getId())){
                                            contactVo.setCustNameOld(caseCustInfo.getCustName());
                                            contactVo.setTelPhoneOld(caseCustInfo.getTelPhone());
                                            contactVo.setBackSign(WhetherEnum.NO.getCode());
                                            contactVo.setReconsiderSign(WhetherEnum.YES.getCode());

                                        }
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }

        return IResponse.success(contactVoList);
    }
    /**
     * queryCommomCustInfo
     * <p>Description: 查询共借人/担保人信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryCommomCustInfo")
    @ApiOperation(value = "按照申请编号查询共借人信息")
    public IResponse<CommonCustInfoVo> queryCommomCustInfo(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {
        CommonCustInfoVo commonCustInfoVo = new CommonCustInfoVo();
        /**  客户信息  **/
        CaseCustInfo caseCustInfo = caseCustInfoService.getOne(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getApplyNo, caseInfoQueryCondition.getApplyNo())
                .eq(CaseCustInfo::getCustRole, caseInfoQueryCondition.getCustRole()));
        if (ObjectUtils.isNotEmpty(caseCustInfo)) {
            BeanUtils.copyProperties(caseCustInfo, commonCustInfoVo);
            commonCustInfoVo.setId(caseCustInfo.getId().toString());
            /**  个人客户信息  **/
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(Wrappers.<CaseCustIndividual>query().lambda()
                    .eq(CaseCustIndividual::getCustId, caseCustInfo.getId()));
            if (ObjectUtils.isNotEmpty(caseCustIndividual)) {
                BeanUtils.copyProperties(caseCustIndividual, commonCustInfoVo);
                commonCustInfoVo.setFamilyIncome(caseCustIndividual.getOtherIncome());
            }
            /**  户籍地址  **/
            List<CaseCustAddress> caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                    .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                    .eq(CaseCustAddress::getAddressType, AddressTypeEnum.CENSUS.getCode()));
            if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                String province = tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getProvince()) != null ? tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getProvince()).getLabel() : "";
                String city = tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getCity()) != null ? tsysAddressParamService.getProvinceOrCtiy(caseCustAddressList.get(0).getCity()).getLabel() : "";
                commonCustInfoVo.setCensus(province + city);
                commonCustInfoVo.setProvince(caseCustAddressList.get(0).getProvince() != null ? caseCustAddressList.get(0).getProvince() : "");
                commonCustInfoVo.setCity(caseCustAddressList.get(0).getCity() != null ? caseCustAddressList.get(0).getCity() : "");
            }
            List list=this.getEvidenceIndividual(caseInfoQueryCondition.getApplyNo(),caseCustInfo);
            if(CollectionUtils.isNotEmpty(list)){
                RedundantSignVo signVo= JSONObject.parseObject(JSONObject.toJSONString(list.get(0)),RedundantSignVo.class);
                CaseCustInfo custInfo= JSONObject.parseObject(JSONObject.toJSONString(list.get(1)),CaseCustInfo.class);
                CaseCustIndividual individual= JSONObject.parseObject(JSONObject.toJSONString(list.get(2)),CaseCustIndividual.class);
                if(ObjectUtils.isNotEmpty(custInfo)){
                    commonCustInfoVo.setTelPhoneOld(custInfo.getTelPhone());
                }
                if(ObjectUtils.isNotEmpty(individual)){
                    commonCustInfoVo.setMaritalStatusOld(individual.getMaritalStatus());
                    commonCustInfoVo.setUnitNameOld(individual.getUnitName());
                }
                commonCustInfoVo.setBackSign(signVo.getBackSign());
                commonCustInfoVo.setReconsiderSign(signVo.getReconsiderSign());
            }

        }
        return IResponse.success(commonCustInfoVo);
    }

    /**
     * queryAddressList
     * <p>Description: 查询地址列表</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryAddressList")
    @ApiOperation(value = "按照申请编号查询地址列表")
    public IResponse<List<AddressVo>> queryAddressList(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {
        /**  按照客户类型获取客户  **/
        CaseCustInfo caseCustInfo = caseCustInfoService.getOne(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getApplyNo, caseInfoQueryCondition.getApplyNo())
                .eq(CaseCustInfo::getCustRole, caseInfoQueryCondition.getCustRole()));
        List<AddressVo> addressVoList = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(caseCustInfo)) {
            /**  地址信息列表  **/
            List<CaseCustAddress> caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                    .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                    .ne(CaseCustAddress::getAddressType, AfsEnumUtil.key(AddressTypeEnum.CENSUS.getCode())));
            //冗余数据
            List<CaseCustAddress> list=getEvidenceForAddress(caseInfoQueryCondition.getApplyNo(),caseCustInfo.getId());
            if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                for (CaseCustAddress caseCustAddress : caseCustAddressList) {
                    AddressVo addressVo = new AddressVo();
                    BeanUtils.copyProperties(caseCustAddress, addressVo);
                    //保存冗余数据
                    if(CollectionUtils.isNotEmpty(list)){
                        RedundantSignVo signVo= JSONObject.parseObject(JSONObject.toJSONString(list.get(0)),RedundantSignVo.class);
                        List<CaseCustAddress> addressList= JSONArray.parseArray(JSONObject.toJSONString(list.get(1)),CaseCustAddress.class);
                        if (CollectionUtil.isNotEmpty(addressList)) {
                            for(CaseCustAddress address : addressList){
                                if(caseCustAddress.getAddressType().equals(address.getAddressType())){
                                    addressVo.setDetailAddressOld(address.getDetailAddress());
                                }
                            }
                        }
                        addressVo.setBackSign(signVo.getBackSign());
                        addressVo.setReconsiderSign(signVo.getReconsiderSign());
                    }
                    String province = tsysAddressParamService.getProvinceOrCtiy(addressVo.getProvince()) != null ? tsysAddressParamService.getProvinceOrCtiy(addressVo.getProvince()).getLabel() : "";
                    String city = tsysAddressParamService.getProvinceOrCtiy(addressVo.getCity()) != null ? tsysAddressParamService.getProvinceOrCtiy(addressVo.getCity()).getLabel() : "";
                    /*String county=tsysAddressParamService.getProvinceOrCtiy(addressVo.getCounty())!=null?tsysAddressParamService.getProvinceOrCtiy(addressVo.getCounty()).getLabel():"";*/
                    addressVo.setProvince(province);
                    addressVo.setCity(city);
                    addressVoList.add(addressVo);
                }
            }


        }
        return IResponse.success(addressVoList);
    }

    /**
     * queryAddressList
     * <p>Description: 查询地址列表</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/removeAddressById")
    @ApiOperation(value = "根据id删除地址")
    public IResponse<String> removeAddressById(@RequestParam("addressId") String id) {
        if (ObjectUtils.isNotEmpty(id)){
            caseCustAddressService.removeById(id);
            return IResponse.success("success");
        }
        return IResponse.fail("error");
    }

    /**
     * saveAddress
     * <p>Description: 新增地址</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveAddress")
    @ApiOperation(value = "保存地址信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveAddress(@ModelAttribute AddressVo addressVo) {
        /**  按照客户id获取客户  **/
        CaseCustInfo caseCustInfo = caseCustInfoService.getById(addressVo.getCustId());
        CaseCustAddress caseCustAddress = new CaseCustAddress();
        BeanUtils.copyProperties(addressVo, caseCustAddress);
        caseCustAddress.setValidStatus(ValidStatusEnum.EFFECTIVE.getCode());
        caseCustAddress.setApplyNo(caseCustInfo.getApplyNo());
        caseCustAddressService.save(caseCustAddress);
        return IResponse.success("客户地址新增成功");
    }

    /**
     * saveAddress
     * <p>Description: 更新客户基本信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveMainInfo")
    @ApiOperation(value = "更新客户基本信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveMainInfo(@ModelAttribute MainBaseInfoVo mainBaseInfoVo) {
        /**  按照客户id获取客户  **/
        if (ObjectUtils.isNotEmpty(mainBaseInfoVo)){
            //更新客户基本信息
            CaseCustInfo caseCustInfo = caseCustInfoService.getById(mainBaseInfoVo.getId());
            caseCustInfo.setNewPhone(mainBaseInfoVo.getNewPhone());
            caseCustInfoService.updateById(caseCustInfo);

            //更新客户个人信息
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(
                    Wrappers.<CaseCustIndividual>lambdaQuery()
                    .eq(CaseCustIndividual::getCustId,Long.parseLong(mainBaseInfoVo.getId()))
            );
            caseCustIndividual.setMaritalStatus(mainBaseInfoVo.getMaritalStatus());
            caseCustIndividual.setWorkAge(mainBaseInfoVo.getWorkAge());
            caseCustIndividual.setDrivingType(mainBaseInfoVo.getDrivingType());
            caseCustIndividual.setDrivingLicenceNo(mainBaseInfoVo.getDrivingLicenceNo());
            caseCustIndividual.setHighestEducation(mainBaseInfoVo.getHighestEducation());
            caseCustIndividualService.updateById(caseCustIndividual);

            //更新户籍地址
            List<CaseCustAddress> caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                    .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                    .eq(CaseCustAddress::getAddressType, AddressTypeEnum.CENSUS.getCode()));
            if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                caseCustAddressList.get(0).setProvince(mainBaseInfoVo.getProvince());
                caseCustAddressList.get(0).setCity(mainBaseInfoVo.getCity());
                caseCustAddressService.updateById(caseCustAddressList.get(0));
            }
            return IResponse.success("更新客户基本信息成功");
        }
        return IResponse.fail("客户信息为空");
    }

    /**
     * saveAddress
     * <p>Description: 更新客户共借人基本信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveCommomCustInfo")
    @ApiOperation(value = "更新客户共借人基本信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveCommomCustInfo(@ModelAttribute CommonCustInfoVo commonCustInfoVo) {
        /**  按照客户id获取客户  **/
        if (ObjectUtils.isNotEmpty(commonCustInfoVo)){
            //更新共借人基本信息
            CaseCustInfo caseCustInfo = caseCustInfoService.getById(commonCustInfoVo.getId());
            caseCustInfo.setNewPhone(commonCustInfoVo.getNewPhone());
            caseCustInfoService.updateById(caseCustInfo);

            //更新共借人个人信息
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(
                    Wrappers.<CaseCustIndividual>lambdaQuery()
                            .eq(CaseCustIndividual::getCustId,Long.parseLong(commonCustInfoVo.getId()))
            );
            caseCustIndividual.setMaritalStatus(commonCustInfoVo.getMaritalStatus());
            caseCustIndividual.setWorkAge(commonCustInfoVo.getWorkAge());
            caseCustIndividual.setHighestEducation(commonCustInfoVo.getHighestEducation());
            caseCustIndividualService.updateById(caseCustIndividual);

            //更新共借人户籍地址
            List<CaseCustAddress> caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                    .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                    .eq(CaseCustAddress::getAddressType, AddressTypeEnum.CENSUS.getCode()));
            if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                caseCustAddressList.get(0).setProvince(commonCustInfoVo.getProvince());
                caseCustAddressList.get(0).setCity(commonCustInfoVo.getCity());
                caseCustAddressService.updateById(caseCustAddressList.get(0));
            }

            return IResponse.success("更新客户基本信息成功");
        }
        return IResponse.fail("客户信息为空");
    }

    /**
     * saveAddress
     * <p>Description: 更新客户担保人基本信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveGuarantorInfo")
    @ApiOperation(value = "更新客户担保人基本信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveGuarantorInfo(@ModelAttribute CommonCustInfoVo commonCustInfoVo) {
        /**  按照客户id获取客户  **/
        if (ObjectUtils.isNotEmpty(commonCustInfoVo)){
            //更新担保人基本信息
            CaseCustInfo caseCustInfo = caseCustInfoService.getById(commonCustInfoVo.getId());
            caseCustInfo.setNewPhone(commonCustInfoVo.getNewPhone());
            caseCustInfoService.updateById(caseCustInfo);

            //更新担保人个人信息
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(
                    Wrappers.<CaseCustIndividual>lambdaQuery()
                            .eq(CaseCustIndividual::getCustId,Long.parseLong(commonCustInfoVo.getId()))
            );
            caseCustIndividual.setMaritalStatus(commonCustInfoVo.getMaritalStatus());
            caseCustIndividual.setWorkAge(commonCustInfoVo.getWorkAge());
            caseCustIndividual.setHighestEducation(commonCustInfoVo.getHighestEducation());
            caseCustIndividualService.updateById(caseCustIndividual);

            //更新担保人户籍地址
            List<CaseCustAddress> caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                    .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                    .eq(CaseCustAddress::getAddressType, AddressTypeEnum.CENSUS.getCode()));
            if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                caseCustAddressList.get(0).setProvince(commonCustInfoVo.getProvince());
                caseCustAddressList.get(0).setCity(commonCustInfoVo.getCity());
                caseCustAddressService.updateById(caseCustAddressList.get(0));
            }



            return IResponse.success("更新客户基本信息成功");
        }
        return IResponse.fail("客户信息为空");
    }

    /**
     * saveAddress
     * <p>Description: 更新客户工作信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveWorkInfo")
    @ApiOperation(value = "更新客户工作信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveWorkInfo(@ModelAttribute MainWorkInfoVo mainWorkInfoVo) {
        if (ObjectUtils.isNotEmpty(mainWorkInfoVo)) {
            //更新客户个人工作信息
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(
                    Wrappers.<CaseCustIndividual>lambdaQuery()
                            .eq(CaseCustIndividual::getCustId,Long.parseLong(mainWorkInfoVo.getId()))
            );
            caseCustIndividual.setUnitName(mainWorkInfoVo.getUnitName());
            caseCustIndividual.setIndustryType(mainWorkInfoVo.getIndustryType());
            caseCustIndividual.setProfessionalType(mainWorkInfoVo.getProfessionalType());
            caseCustIndividual.setPosition(mainWorkInfoVo.getPosition());
            caseCustIndividual.setUnitTelPhone(mainWorkInfoVo.getUnitTelPhone());
            caseCustIndividual.setMonthlyIncome(mainWorkInfoVo.getMonthlyIncome());
            caseCustIndividual.setOutstandingLoan(mainWorkInfoVo.getOutstandingLoan());
            caseCustIndividual.setOtherIncome(mainWorkInfoVo.getFamilyIncome());

            caseCustIndividualService.updateById(caseCustIndividual);
            return IResponse.success("更新客户基本信息成功");
        }
        return IResponse.fail("客户信息为空");
    }

    /**
     * saveAddress
     * <p>Description: 更新共借人工作信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveCommonWorkInfo")
    @ApiOperation(value = "更新共借人工作信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveCommonWorkInfo(@ModelAttribute CommonCustInfoVo commonCustInfoVo) {
        if (ObjectUtils.isNotEmpty(commonCustInfoVo)) {
            //更新客户个人工作信息
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(
                    Wrappers.<CaseCustIndividual>lambdaQuery()
                            .eq(CaseCustIndividual::getCustId,Long.parseLong(commonCustInfoVo.getId()))
            );
            caseCustIndividual.setUnitName(commonCustInfoVo.getUnitName());
            caseCustIndividual.setIndustryType(commonCustInfoVo.getIndustryType());
            caseCustIndividual.setProfessionalType(commonCustInfoVo.getProfessionalType());
            caseCustIndividual.setPosition(commonCustInfoVo.getPosition());
            caseCustIndividual.setUnitTelPhone(commonCustInfoVo.getUnitTelPhone());
            caseCustIndividual.setMonthlyIncome(commonCustInfoVo.getMonthlyIncome());
            caseCustIndividual.setOutstandingLoan(commonCustInfoVo.getOutstandingLoan());
            caseCustIndividual.setOtherIncome(commonCustInfoVo.getFamilyIncome());

            caseCustIndividualService.updateById(caseCustIndividual);
            return IResponse.success("更新客户基本信息成功");
        }
        return IResponse.fail("客户信息为空");
    }

    /**
     * saveAddress
     * <p>Description: 更新担保人工作信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveGuarantorWorkInfo")
    @ApiOperation(value = "更新担保人工作信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveGuarantorWorkInfo(@ModelAttribute CommonCustInfoVo commonCustInfoVo) {
        if (ObjectUtils.isNotEmpty(commonCustInfoVo)) {
            //更新客户个人工作信息
            CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(
                    Wrappers.<CaseCustIndividual>lambdaQuery()
                            .eq(CaseCustIndividual::getCustId,Long.parseLong(commonCustInfoVo.getId()))
            );
            caseCustIndividual.setUnitName(commonCustInfoVo.getUnitName());
            caseCustIndividual.setIndustryType(commonCustInfoVo.getIndustryType());
            caseCustIndividual.setProfessionalType(commonCustInfoVo.getProfessionalType());
            caseCustIndividual.setPosition(commonCustInfoVo.getPosition());
            caseCustIndividual.setUnitTelPhone(commonCustInfoVo.getUnitTelPhone());
            caseCustIndividual.setMonthlyIncome(commonCustInfoVo.getMonthlyIncome());
            caseCustIndividual.setOutstandingLoan(commonCustInfoVo.getOutstandingLoan());
            caseCustIndividual.setOtherIncome(commonCustInfoVo.getFamilyIncome());

            caseCustIndividualService.updateById(caseCustIndividual);
            return IResponse.success("更新客户基本信息成功");
        }
        return IResponse.fail("客户信息为空");
    }
    /**
     * saveContact
     * <p>Description: 新增地址</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/saveContact")
    @ApiOperation(value = "保存联系人信息")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> saveContact(@ModelAttribute ContactVo contactVo) {
        /**  按照客户类型获取客户  **/
        CaseCustContact caseCustContact = new CaseCustContact();
        BeanUtils.copyProperties(contactVo, caseCustContact);
        caseCustContact.setValidStatus(ValidStatusEnum.EFFECTIVE.getCode());
        caseCustContact.setIsDefault(WhetherEnum.NO.getCode());
        caseCustContactService.save(caseCustContact);
        return IResponse.success("保存联系人信息");
    }

    /**
     * queryContactList
     * <p>Description: 查询联系人列表</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryContactListOld")
    @ApiOperation(value = "按照申请编号查询冗余联系人列表")
    public IResponse<List<ContactVo>> queryContactListOld(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {
        List<ContactVo> contactVoList = new ArrayList<>();
        /**  联系人信息列表  **/
        List<CaseCustContact> caseCustContactList = this.getEvidenceForContact(caseInfoQueryCondition.getApplyNo());

        if (CollectionUtils.isNotEmpty(caseCustContactList)) {
            for (CaseCustContact caseCustContact : caseCustContactList) {
                ContactVo contactVo = new ContactVo();
                BeanUtils.copyProperties(caseCustContact, contactVo);
                contactVoList.add(contactVo);
            }
        }
        return IResponse.success(contactVoList);
    }
    /**
     * @description: 根据id删除联系人
     * @param id
     * @return
     */
    @PostMapping(value = "/removeContactById")
    @ApiOperation(value = "根据id删除联系人")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<String> removeContactById(@RequestParam("contactId") String id){
        if (ObjectUtils.isNotEmpty(id)){
            caseCustContactService.removeById(id);
            return IResponse.success("success");
        }
        return IResponse.fail("error");
    }

    /**
    * @Description 征信报告主借人信息查询
    * @Author Peng_Jinbo
    * @Date 2020/9/9 20:28
    */
    @GetMapping("/creditCustInfo")
    public IResponse getCustInfo(@ModelAttribute CreditCustomerCondition condition){
        final String TYPE_NORMAL = "1"; // 常规
        final String TYPE_PREV = "2";   // 预审
        CreditCustomerInfoVO customerInfoVO = CreditCustomerInfoVO.builder().build();
        if(StrUtil.equals(condition.getType(),TYPE_NORMAL)){
            CaseCustInfo custInfo = caseCustInfoService.getOne(
                    Wrappers.<CaseCustInfo>lambdaQuery()
                        .eq(CaseCustInfo::getApplyNo,condition.getApplyNo())
                        .eq(CaseCustInfo::getCustRole,CustRoleEnum.MIANCUST.getCode())
            );
            if(Objects.nonNull(custInfo)){
                customerInfoVO.setCustName(custInfo.getCustName());
                customerInfoVO.setCertNo(custInfo.getCertNo());
            }
        }else if(StrUtil.equals(condition.getType(),TYPE_PREV)){
            CaseApprovePrevInfo prevInfo = caseApprovePrevInfoService.getOne(
                    Wrappers.<CaseApprovePrevInfo>lambdaQuery()
                        .eq(CaseApprovePrevInfo::getApplyNo,condition.getApplyNo())
            );
            if(Objects.nonNull(prevInfo)){
                customerInfoVO.setCustName(prevInfo.getCustName());
                customerInfoVO.setCertNo(prevInfo.getCertNo());
            }
        }
        return IResponse.success(customerInfoVO);
    }

    /**
     * queryCustSummaryInfoApplyNo
     * <p>Description: 根据申请编号查询客户概况信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryCustSummaryInfoApplyNo")
    @ApiOperation(value = "根据申请编号查询客户概况信息")
    public IResponse<CustInfoVo> queryCustSummaryInfoApplyNo(@ModelAttribute WorkTaskPoolCondition workTaskPoolCondition) {
        CustInfoVo custInfoVo = new CustInfoVo();
        CaseCarInfo caseCarInfo = caseCarInfoService.getOne(Wrappers.<CaseCarInfo>query().lambda()
                .eq(CaseCarInfo::getApplyNo, workTaskPoolCondition.getApplyNo())
        );
        CaseBaseInfo caseBaseInfo = caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda()
                .eq(CaseBaseInfo::getApplyNo, workTaskPoolCondition.getApplyNo())
        );
        if (ObjectUtils.isNotEmpty(custInfoVo)) {
            /**  车型  **/
            custInfoVo.setModelName(caseCarInfo.getModelName());
            /**  首付比例 贷款期限 贷款金额 **/
            FinCostDetails finCostInfo = caseCostInfoService.getOne(Wrappers.<FinCostDetails>query().lambda()
                    .eq(FinCostDetails::getApplyNo, caseCarInfo.getApplyNo())
                    .eq(FinCostDetails::getCostType, CostTypeEnum.CARAMT.getCode())
            );
            if (ObjectUtils.isNotEmpty(finCostInfo)) {
                custInfoVo.setDownPayScale(finCostInfo.getDownPayScale());
                custInfoVo.setLoanTerms(finCostInfo.getLoanTerm());
                custInfoVo.setLoanAmt(caseBaseInfo.getLoanAmtRepeat());
            }
        }
        /**  共借人担保人信息  **/
        List<CaseCustInfo> caseCustInfoList = caseCustInfoService.list(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getApplyNo, workTaskPoolCondition.getApplyNo())
        );
        List<CustSummaryInfoVo> custSummaryInfoVoList = new ArrayList<>();
        String borrowerCertno = "";//主借人身份证号
        String coborrowerCertno = "";//共借人身份证号
        if (CollectionUtils.isNotEmpty(caseCustInfoList)) {
            for (CaseCustInfo caseCustInfo : caseCustInfoList) {

                CustSummaryInfoVo custSummaryInfoVo = new CustSummaryInfoVo();
                custSummaryInfoVo.setCustRelation(caseCustInfo.getCustRelation());
                custSummaryInfoVo.setCustName(caseCustInfo.getCustName());

                CaseCustIndividual caseCustIndividual = caseCustIndividualService.getOne(Wrappers.<CaseCustIndividual>query().lambda()
                        .eq(CaseCustIndividual::getCustId, caseCustInfo.getId())
                );
                if (ObjectUtils.isNotEmpty(caseCustIndividual)) {
                    custSummaryInfoVo.setAge(caseCustIndividual.getAge());
                    custSummaryInfoVo.setUnitName(caseCustIndividual.getUnitName());
                    custSummaryInfoVo.setSex(caseCustIndividual.getSex());
                    custSummaryInfoVo.setMaritalStatus(caseCustIndividual.getMaritalStatus());
                }
                    String province = tsysAddressParamService.getProvinceOrCtiy(caseCarInfo.getPurchaseProvince()) != null ? tsysAddressParamService.getProvinceOrCtiy(caseCarInfo.getPurchaseProvince()).getLabel() : "";
                    String city = tsysAddressParamService.getProvinceOrCtiy(caseCarInfo.getPurchaseCity()) != null ? tsysAddressParamService.getProvinceOrCtiy(caseCarInfo.getPurchaseCity()).getLabel() : "";
                    /**  购车地址  **/
                    //只取城市
                    custSummaryInfoVo.setPurchaseAddress(city);

                /**  居住地、工作地  **/
                List<CaseCustAddress> caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                        .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                        .eq(CaseCustAddress::getAddressType, AddressTypeEnum.LIVING.getCode())
                        .orderByDesc(CaseCustAddress::getCreateTime)
                );
                if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                    CaseCustAddress address = caseCustAddressList.get(0);
                    custSummaryInfoVo.setLivingAddress(address.getDetailAddress());
                }
                caseCustAddressList = caseCustAddressService.list(Wrappers.<CaseCustAddress>query().lambda()
                        .eq(CaseCustAddress::getCustId, caseCustInfo.getId())
                        .eq(CaseCustAddress::getAddressType, AddressTypeEnum.WORK.getCode())
                        .orderByDesc(CaseCustAddress::getCreateTime)
                );
                if (CollectionUtils.isNotEmpty(caseCustAddressList)) {
                    CaseCustAddress address = caseCustAddressList.get(0);
                    custSummaryInfoVo.setWorkAddress(address.getDetailAddress());
                }
                //主借人
                if (caseCustInfo.getCustRole().equals(AfsEnumUtil.key(CustRoleEnum.MIANCUST.getCode()))) {
                    borrowerCertno = caseCustInfo.getCertNo();
                } else if (caseCustInfo.getCustRole().equals(AfsEnumUtil.key(CustRoleEnum.COMMONCSUT.getCode()))) {
                    coborrowerCertno = caseCustInfo.getCertNo();
                }
                custSummaryInfoVoList.add(custSummaryInfoVo);
            }
        }

        /**  TODO GJQ 累加贷款总额  **/
        CertNoDto certNoDto = new CertNoDto();
        certNoDto.setBorrowerCertno(borrowerCertno);
        certNoDto.setCoborrowerCertno(coborrowerCertno);
        custInfoVo.setTotalLoanAmt(caseBaseInfo.getTotalLoanAmt());
        custInfoVo.setCustInfoList(custSummaryInfoVoList);

        return IResponse.success(custInfoVo);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * @description: 解析客户信息详情冗余数据
     * @author jiaqi.guo
     * @created 2020/8/16 20:27
     * @version 1.0
     */
    public CaseCustIndividual getEvidenceForCustIndividual(String jsonStr, Long id) {
        CaseCustIndividual caseCust = new CaseCustIndividual();
        if (StringUtils.isNotBlank(jsonStr)) {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            JSONArray caseCustIndividualList = jsonObject.getJSONArray("caseCustIndividualList");
            if (ObjectUtils.isNotEmpty(caseCustIndividualList) && caseCustIndividualList.size() > 0) {
                caseCustIndividualList.toJavaList(CaseCustIndividual.class).forEach(caseCustIndividual -> {
                    if (caseCustIndividual.getCustId().equals(id)) {
                        BeanUtils.copyProperties(caseCustIndividual, caseCust);
                    }
                });
            }
        }
        return caseCust;
    }
    /**
     * @description: 解析客户信息详情冗余数据
     * @author jiaqi.guo
     * @created 2020/8/16 20:27
     * @version 1.0
     */
    public CaseCustInfo getEvidenceForCustInfo(String jsonStr, Long id) {
        CaseCustInfo caseCust = new CaseCustInfo();
        if (StringUtils.isNotBlank(jsonStr)) {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            JSONArray caseCustList = jsonObject.getJSONArray("caseCustInfoList");
            if (ObjectUtils.isNotEmpty(caseCustList) && caseCustList.size() > 0) {
                caseCustList.toJavaList(CaseCustInfo.class).forEach(caseCustInfo -> {
                    if (caseCustInfo.getId().equals(id)) {
                        BeanUtils.copyProperties(caseCustInfo, caseCust);
                    }
                });
            }
        }
        return caseCust;
    }
    /**
     * @description: 解析联系人冗余数据
     * @author jiaqi.guo
     * @created 2020/8/16 20:27
     * @version 1.0
     */
    public List<CaseCustAddress> getEvidenceAddress(String jsonStr, Long id) {
       List<CaseCustAddress> list= new ArrayList();
        if (StringUtils.isNotBlank(jsonStr)) {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            JSONArray addressList = jsonObject.getJSONArray("custAddressList");
            if (ObjectUtils.isNotEmpty(addressList) && addressList.size() > 0) {
                addressList.toJavaList(CaseCustAddress.class).forEach(address -> {
                    if(address.getCustId().equals(id)&&!address.getAddressType().equals(AfsEnumUtil.key(AddressTypeEnum.CENSUS.getCode()))){
                        CaseCustAddress custAddress = new CaseCustAddress();
                        BeanUtils.copyProperties(address, custAddress);
                        list.add(custAddress);
                    }
                });
            }
        }

        return list;
    }
    /**
     * @description: 解析联系人冗余数据
     * @author jiaqi.guo
     * @created 2020/8/16 20:27
     * @version 1.0
     */
    public List<CaseCustContact> getEvidenceContractInfo(String jsonStr) {
        List<CaseCustContact> list= new ArrayList();
        if (StringUtils.isNotBlank(jsonStr)) {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            JSONArray caseContactList = jsonObject.getJSONArray("caseContactList");
            if (ObjectUtils.isNotEmpty(caseContactList) && caseContactList.size() > 0) {
                list=caseContactList.toJavaList(CaseCustContact.class);
            }
        }

        return list;
    }
    /**
     * @description: 获取冗余数据
     * @author jiaqi.guo
     * @created 2020/8/16 21:42
     * @version 1.0
     */
    public List getEvidenceIndividual(String applyNo, CaseCustInfo caseCustInfo) {

        RedundantSignVo signVo= new RedundantSignVo();
        signVo.setBackSign(WhetherEnum.NO.getCode());
        signVo.setReconsiderSign(WhetherEnum.NO.getCode());
        CaseCustInfo caseCust = null;
        CaseCustIndividual custIndividual = null;
        List list= new ArrayList();
        CaseRedundantInfo redundantInfo = redundantInfoService.getOne(Wrappers.<CaseRedundantInfo>query().lambda()
                .eq(StringUtils.isNotBlank(applyNo), CaseRedundantInfo::getApplyNo, applyNo));
        if (ObjectUtils.isNotEmpty(redundantInfo)) {
            String backSign = redundantInfo.getBackSign();
            if (WhetherEnum.YES.getCode().equals(backSign)) {
                String backEvidence = redundantInfo.getBackEvidence();
                if (StringUtils.isNotBlank(backEvidence)) {
                    caseCust = this.getEvidenceForCustInfo(backEvidence, caseCustInfo.getId());
                    custIndividual = this.getEvidenceForCustIndividual(backEvidence, caseCustInfo.getId());
                    signVo.setBackSign(WhetherEnum.YES.getCode());
                    signVo.setReconsiderSign(WhetherEnum.NO.getCode());
                }
            }
            String reconsiderSign = redundantInfo.getReconsiderSign();
            if (WhetherEnum.YES.getCode().equals(reconsiderSign)) {
                String reconsiderEvidence = redundantInfo.getReconsiderEvidence();
                if (StringUtils.isNotBlank(reconsiderEvidence)) {
                    caseCust = this.getEvidenceForCustInfo(reconsiderEvidence, caseCustInfo.getId());
                    custIndividual = this.getEvidenceForCustIndividual(reconsiderEvidence, caseCustInfo.getId());
                    signVo.setBackSign(WhetherEnum.NO.getCode());
                    signVo.setReconsiderSign(WhetherEnum.YES.getCode());
                }
            }
        }
        list.add(signVo);
        list.add(caseCust);
        list.add(custIndividual);
        return list;
    }

    /**
     * @description: 获取地址冗余数据
     * @author jiaqi.guo
     * @created 2020/8/16 21:42
     * @version 1.0
     */
    public List<CaseCustAddress> getEvidenceForAddress(String applyNo, Long id ) {
        List list= new ArrayList();
        List<CaseCustAddress> addressList = new ArrayList<>();
        RedundantSignVo signVo= new RedundantSignVo();
        signVo.setBackSign(WhetherEnum.NO.getCode());
        signVo.setReconsiderSign(WhetherEnum.NO.getCode());
        CaseRedundantInfo redundantInfo = redundantInfoService.getOne(Wrappers.<CaseRedundantInfo>query().lambda()
                .eq(StringUtils.isNotBlank(applyNo), CaseRedundantInfo::getApplyNo, applyNo));
        if (ObjectUtils.isNotEmpty(redundantInfo)) {
            String backSign = redundantInfo.getBackSign();
            if (WhetherEnum.YES.getCode().equals(backSign)) {
                String backEvidence = redundantInfo.getBackEvidence();
                if (StringUtils.isNotBlank(backEvidence)) {
                    addressList = this.getEvidenceAddress(backEvidence,id);
                    signVo.setBackSign(WhetherEnum.YES.getCode());
                    signVo.setReconsiderSign(WhetherEnum.NO.getCode());
                }
            }
            String reconsiderSign = redundantInfo.getReconsiderSign();
            if (WhetherEnum.YES.getCode().equals(reconsiderSign)) {
                String reconsiderEvidence = redundantInfo.getReconsiderEvidence();
                if (StringUtils.isNotBlank(reconsiderEvidence)) {
                    addressList = this.getEvidenceAddress(reconsiderEvidence, id);
                    signVo.setBackSign(WhetherEnum.NO.getCode());
                    signVo.setReconsiderSign(WhetherEnum.YES.getCode());
                }
            }
        }
        list.add(signVo);
        list.add(addressList);
        return list;
    }

    /**
     * @description: 获取联系人冗余数据
     * @author jiaqi.guo
     * @created 2020/8/16 21:42
     * @version 1.0
     */
    public List<CaseCustContact> getEvidenceForContact(String applyNo) {
        List<CaseCustContact> list= new ArrayList();
        CaseRedundantInfo redundantInfo = redundantInfoService.getOne(Wrappers.<CaseRedundantInfo>query().lambda()
                .eq(StringUtils.isNotBlank(applyNo), CaseRedundantInfo::getApplyNo, applyNo));
        if (ObjectUtils.isNotEmpty(redundantInfo)) {
            String backSign = redundantInfo.getBackSign();
            if (WhetherEnum.YES.getCode().equals(backSign)) {
                String backEvidence = redundantInfo.getBackEvidence();
                if (StringUtils.isNotBlank(backEvidence)) {
                    list = this.getEvidenceContractInfo(backEvidence);
                }
            }
            String reconsiderSign = redundantInfo.getReconsiderSign();
            if (WhetherEnum.YES.getCode().equals(reconsiderSign)) {
                String reconsiderEvidence = redundantInfo.getReconsiderEvidence();
                if (StringUtils.isNotBlank(reconsiderEvidence)) {
                    list = this.getEvidenceContractInfo(reconsiderEvidence);
                }
            }
        }
        return list;
    }


    @PostMapping(value = "/getCustomerDetailInfo")
    @ApiOperation(value = "根据申请编号获取客户详细信息")
    public IResponse<InterfaceInfoVo> getCustomerDetailInfo(@RequestParam(value = "applyNo") String applyNo) {

        //01-主借人、02-共借人、03-担保人
        String mainRole = "01";
        String commonRole = "02";
        String assureRole = "03";
        String man = "M";
        CustomerInfoVo mainCustomerInfoVo = caseCustInfoService.getCustomerInfo(applyNo,mainRole);
        CustomerInfoVo commonCustomerInfoVo = caseCustInfoService.getCustomerInfo(applyNo,commonRole);
        CustomerInfoVo assureCustomerInfoVo = caseCustInfoService.getCustomerInfo(applyNo,assureRole);
        InterfaceInfoVo otherInfo = caseCustInfoService.getCustomerOtherInfo(applyNo);
        List<CaseContractInfo> caseContractInfos = caseContractInfoService.list(Wrappers.<CaseContractInfo>query().lambda().eq(CaseContractInfo::getApplyNo, applyNo));
        if(CollectionUtil.isNotEmpty(caseContractInfos)){
            ChannelWitnessInfo channelWitnessInfo = channelWitnessInfoService.getOne(Wrappers.<ChannelWitnessInfo>query().lambda()
                    .eq(ChannelWitnessInfo::getChannelId, otherInfo.getChannelId())
                    .eq(ChannelWitnessInfo::getWitnessIdCard, caseContractInfos.get(0).getWitnessId())
                    .eq(ChannelWitnessInfo::getBusinessAttributes, caseContractInfos.get(0).getBusinessType()));
            if (channelWitnessInfo!=null){
                otherInfo.setOperator_user(channelWitnessInfo.getWitnessName());
            }
        }

        InterfaceInfoVo interfaceInfoVo = new InterfaceInfoVo();
        if(mainCustomerInfoVo!=null) {
            interfaceInfoVo.setBorrower(mainCustomerInfoVo.getCustName());
            interfaceInfoVo.setBorrower_id_card_type(mainCustomerInfoVo.getCertType());
            interfaceInfoVo.setBorrower_id_card(mainCustomerInfoVo.getCertNo());
            interfaceInfoVo.setBorrower_phone(mainCustomerInfoVo.getTelPhone());
            interfaceInfoVo.setBorrower_address(mainCustomerInfoVo.getDetailAddress());
            interfaceInfoVo.setBorrower_sex(man.equals(mainCustomerInfoVo.getSex()) ? SexEnum.man.toString() : SexEnum.woman.toString());
            interfaceInfoVo.setBorrower_marriage(mainCustomerInfoVo.getMaritalStatus());
        }
        if(commonCustomerInfoVo!=null) {
            interfaceInfoVo.setCoborrower(commonCustomerInfoVo.getCustName());
            interfaceInfoVo.setCoborrower_id_card_type(commonCustomerInfoVo.getCertType());
            interfaceInfoVo.setCoborrower_id_card(commonCustomerInfoVo.getCertNo());
            interfaceInfoVo.setCoborrower_phone(commonCustomerInfoVo.getTelPhone());
            interfaceInfoVo.setCoborrower_sex(man.equals(commonCustomerInfoVo.getSex()) ? SexEnum.man.toString() : SexEnum.woman.toString());
            interfaceInfoVo.setCoborrower_relation(commonCustomerInfoVo.getCustRelation());
            interfaceInfoVo.setCoborrower_address(commonCustomerInfoVo.getDetailAddress());
        }
        if(assureCustomerInfoVo!=null) {
            interfaceInfoVo.setGuarantor(assureCustomerInfoVo.getCustName());
            interfaceInfoVo.setGuarantor_id_card_type(assureCustomerInfoVo.getCertType());
            interfaceInfoVo.setGuarantor_id_card(assureCustomerInfoVo.getCertNo());
            interfaceInfoVo.setGuarantor_phone(assureCustomerInfoVo.getTelPhone());
            interfaceInfoVo.setGuarantor_sex(man.equals(assureCustomerInfoVo.getSex()) ? SexEnum.man.toString() : SexEnum.woman.toString());
            interfaceInfoVo.setGuarantor_relation(assureCustomerInfoVo.getCustRelation());
            interfaceInfoVo.setGuarantor_address(assureCustomerInfoVo.getDetailAddress());
        }
        if(otherInfo != null){
            interfaceInfoVo.setApplication_no(applyNo);
            interfaceInfoVo.setApplication_status(otherInfo.getApplication_status());
            interfaceInfoVo.setApplication_date(otherInfo.getApplication_date());
            interfaceInfoVo.setDealer(otherInfo.getDealer());
            interfaceInfoVo.setDealer_address(otherInfo.getDealer_address());
            interfaceInfoVo.setDealer_phone(otherInfo.getDealer_phone());
            interfaceInfoVo.setOperator_user(otherInfo.getOperator_user());
            interfaceInfoVo.setAsset_condition(otherInfo.getAsset_condition());
            interfaceInfoVo.setMaker(otherInfo.getMaker());
            interfaceInfoVo.setBrand(otherInfo.getBrand());
            interfaceInfoVo.setModel(otherInfo.getModel());
            interfaceInfoVo.setKinetic_parameter(otherInfo.getKinetic_parameter());
            interfaceInfoVo.setVin(otherInfo.getVin());
            interfaceInfoVo.setColor(otherInfo.getColor());
            interfaceInfoVo.setPrice(otherInfo.getPrice());
        }
        return IResponse.success(interfaceInfoVo);
    }


    @PostMapping(value = "/getCustomerInfo")
    @ApiOperation(value = "根据申请编号获取客户信息")
    public IResponse<BasicCustBaseInfoVo> getCustomerInfo(@RequestParam(value = "applyNo",required = false) String applyNo) {
        log.info("------------------根据申请编号获取客户信息入参：[{}]------------------",applyNo);
        if(StringUtils.isBlank(applyNo)||"null".equals(applyNo)){
            return IResponse.fail("申请编号入参不能为空！");
        }
        //01-主借人、02-共借人、03-担保人
        String mainRole = "01";
        //合同激活
        String activate = "activate";
        //获取主借人信息
        List<CaseCustInfo> custInfos = caseCustInfoService.list(Wrappers.<CaseCustInfo>query().lambda()
                .eq(CaseCustInfo::getCustRole, mainRole)
                .and(wrapper1 -> wrapper1.eq(CaseCustInfo::getApplyNo, applyNo)
                        .or(wrapper2 -> wrapper2.eq(CaseCustInfo::getCertNo, applyNo))
                        .or(wrapper3 -> wrapper3.eq(CaseCustInfo::getTelPhone, applyNo))));
        if(custInfos!= null && custInfos.size() >0){
            CaseCustInfo custInfo = custInfos.get(0);
            //获取银行卡信息
            List<String> applyNos = new ArrayList<>();
            for(CaseCustInfo caseCustInfo : custInfos){
                applyNos.add(caseCustInfo.getApplyNo());
            }
            List<CustBaseInfoVo> bankInfos = caseCustInfoService.getBankInfoByApplyNo(applyNos);
            if(bankInfos != null && bankInfos.size() > 0){
                int size = bankInfos.size();
                int removeSize = 0;
                for(int i =bankInfos.size()-1;i>=0;i-- ){
                    LoanActivatePool pool = loanActivateService.getOne(Wrappers.<LoanActivatePool>query().lambda()
                            .eq(LoanActivatePool::getContractNo, bankInfos.get(i).getContractNo()));
                    if(mainRole.equals(bankInfos.get(i).getCustRole())){
                        bankInfos.get(i).setDefaultFlag("1");
                    }else{
                        bankInfos.get(i).setDefaultFlag("0");
                    }
                    if(pool != null && activate.equals(pool.getActStatus())){
                        bankInfos.remove(i);
                        removeSize++;
                        applyNos.remove(pool.getApplyNo());
                    }
                }
                if(removeSize == size){
                    log.info("----------------------查询到的合同均已激活,不返回数据--------------------");
                    return IResponse.fail("查询到的合同均已激活,不返回数据");
                }
            }
            BasicCustBaseInfoVo basicCustBaseInfoVo = new BasicCustBaseInfoVo();
            if(CollectionUtils.isNotEmpty(bankInfos)){
                basicCustBaseInfoVo.setCustBaseInfoList(bankInfos);
                basicCustBaseInfoVo.setLoanNum(String.valueOf(bankInfos.size()));
            }else{
                List<CustBaseInfoVo> vos = new ArrayList<>();
                for (String applyNum : applyNos){
                    CustBaseInfoVo vo = new CustBaseInfoVo();
                    CaseBaseInfo one = caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda().eq(CaseBaseInfo::getApplyNo, applyNum));
                    vo.setContractNo(applyNum);
                    vo.setContractStatus(one.getBusinessStateIn());
                    /*************角色类型查不到,暂时给空值**************/
                    vo.setCustRole(null);
                    vos.add(vo);
                }
                basicCustBaseInfoVo.setCustBaseInfoList(vos);
                basicCustBaseInfoVo.setLoanNum(String.valueOf(applyNos.size()));
            }
            basicCustBaseInfoVo.setCustName(custInfo.getCustName());
            basicCustBaseInfoVo.setPhone(custInfo.getTelPhone());
            basicCustBaseInfoVo.setCertNo(custInfo.getCertNo());
            basicCustBaseInfoVo.setCustRole(custInfo.getCustRole());
            log.info("------------------根据申请编号获取客户信息查询到的数据为：[{}]------------------",basicCustBaseInfoVo);
            return IResponse.success(basicCustBaseInfoVo);
        }
        log.info("------------------查询不到主借人信息,不返回数据------------------");
        return IResponse.fail("查询结果不存在");
    }


}